import React,{useState,useEffect,useRef} from 'react'

function useHook(n) {
    const [num, setNum] = useState(n)
    let timer 
    useEffect(() => {
        if(!num){  //  num == 0 的时候 就清除定时器
            clearTimeout(timer) 
            timer = null
        } else{
            timer = setTimeout(() => {
                setNum((num) => num - 1)
            }, 1000)
        }
        return () => {
            clearTimeout(timer) 
            timer = null
        }
    }, [num])
    return num
}

export default useHook

// function Timer(n) {
//     // const time = useHook(n);
    
//     // return <div>
//     //         {time === 0 ? ('loaded'+<button>按钮</button>) : `loading ${time}秒`}
//     //     </div>

//     return useHook(n)
//   }
  
  
//   export default Timer